package cn.edu.jju.stepcounter2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import static android.util.Log.i;

/**
 * Created by roor on 2016/12/14.
 */

public class BarGraphView extends View {

    private int height;
    private int width;
    private Paint test;
    private float moveX;
    private float x;

    public BarGraphView(Context context) {
        super(context);
        init();
    }

    public BarGraphView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BarGraphView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);      //取出宽度的确切数值
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);      //取出宽度的测量模式

        int heightsize = MeasureSpec.getSize(heightMeasureSpec);    //取出高度的确切数值
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);    //取出高度的测量模式
        if (widthmode == MeasureSpec.EXACTLY) {
            width = widthsize;
        } else {
            width = 300;
        }
        if (heightmode == MeasureSpec.EXACTLY) {
            height = heightsize;
        } else {
            height = 300;
        }
        i("@@@@@@", "onMeasure" + height + "----" + width);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.GRAY);
        canvas.save();
        canvas.translate(width / 2 + moveX, height / 2);
        canvas.drawLine(0, 0, -1000, 0, test);
        canvas.drawLine(0, 0, -50, -60, test);
        canvas.restore();
        canvas.drawLine(0, 0, 30, 20, test);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                float y = event.getY();
                i("@@@@", "onTouchEvent" + x + "---" + y);
                break;
            case MotionEvent.ACTION_MOVE:
                float newX = event.getX();
                float newY = event.getY();
                moveX += newX - x;
                if (moveX > 1000) {
                    moveX = 1000;
                }
                if (!(moveX < 0)) {
                    invalidate();
                    x = newX;
                }



                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    private void init() {
        test = new Paint();
        test.setColor(Color.BLACK);
    }


}
